$(function(){
    //键盘商品搜索
   // $('#seek_shop').keyup(function(){
   //      var _value = $('#seek_shop').val();
   //      $.ajax({
   //          // url:BaseUrl +'/api/good/list?name='+_value+'&page=1&limit=50',
   //          url:BaseUrl +'/api/good/list?name='+_value,
   //          type:'get',
   //          // data:{
   //          //     q:_value
   //          // },
   //          success:function(response){
   //              // $('#container').empty();
   //              // $.each(response.result, function(index,value) {
   //              //     var $li = $('<li></li>');
   //              //     $li.text(value[0]);
   //              //     var $span = $('<span></span>');
   //              //     $span.text(value[1]);
   //              //     $li.append($span);
   //              //     $('#container').append($li);
   //              // });
   //          }
   //      })
   //  });
    //输入框放大镜点击商品搜索
    $("#seek_button").click(function () {
        //浏览器传递参数
        var searchText = jQuery.trim($("#seek_shop").val());
        var searchUrl = encodeURI("/template/goods.html?searchText=" + searchText); //使用encodeURI编码
        location.href = searchUrl;
    });


    //nav 导航点击跳转
    $("#xihuan_shop").click(function () {
        //浏览器传递参数
        var searchText = jQuery.trim('allshop');
        var searchUrl = encodeURI("/template/goods.html?searchText=" + searchText); //使用encodeURI编码
        location.href = searchUrl;
    });

// 数据交互
     $(document).ready( function(){
        //轮播图
        $.ajax({
            url:BaseUrl +"/api/index/carouse",
            type:"get",
            // data:{},
            success:function(bannerImg) {
                var bannerHtml = template("banner",bannerImg);
                $(".swiper-wrapper").html(bannerHtml);

                //轮播插件自动播放
                var mySwiper = new Swiper ('.swiper-container', {
                    direction: 'horizontal',
                    loop: true,
                    autoplay: 3000,

                    // 如果需要分页器
                    pagination: '.swiper-pagination',
                    // 如果需要前进后退按钮
                     nextButton: '.swiper-button-next',
                     prevButton: '.swiper-button-prev',
                    // 如果需要滚动条
                    //  scrollbar: '.swiper-scrollbar',
                });

                //商品分类
                $.ajax({
                    url:BaseUrl +"/api/index/goodType/list",
                    type:"get",
                    // data:{},
                    success:function(allTypes) {
                        console.log(allTypes);
                        var sort_data = allTypes.data;
                        var arr=[];
                        for (var i = 0; i < sort_data.length; i++){
                            var sort_dataID = sort_data[i].pid;
                            if (sort_dataID==0){
                                arr.push(sort_data[i]);
                            }
                        }
                        allTypes.arrow = arr;
                        var allTypesHtml = template("classify",allTypes);
                        $(".category-list").html(allTypesHtml);

                        //全部分类点击 切换
                        $(".all-category").click(function(){
                            $(".category-list").toggle();
                        });
                    }
                });
            }
        });

        //明星单品
        $.ajax({
            url:BaseUrl +"/api/index/hot?limit=5",
            type:"get",
            // data:{},
            success:function(starGoods) {
                var starGoodsHtml = template("star",starGoods);
                $(".list1 .list-title").append(starGoodsHtml);
            }
        });

       // 海报图
        $.ajax({
            url:BaseUrl +"/api/index/longAdimg",
            type:"get",
            success:function(longAdimg) {
                var longAdimgHtml = template("longAdimg",longAdimg);
                $(".list2-in").html(longAdimgHtml);
                var goodListHtml;

               var goodtypeid = longAdimg.data[0].types[0].pid;
                if (goodtypeid ==0 ) goodtypeid = longAdimg.data[0].types[0].id;
                Ajax(0, '/api/good/list?limit=8&page=1&pid=' + goodtypeid, 'get').then(function (goodList) {
                   goodListHtml = template("goodListTmp", goodList);
                    $(".list2-product-0 .list2-right").html(goodListHtml);
                });

                goodtypeid = longAdimg.data[1].types[0].pid;
                if (goodtypeid ==0 ) goodtypeid = longAdimg.data[1].types[0].id;
                Ajax(0, '/api/good/list?limit=8&page=1&pid=' + goodtypeid, 'get').then(function (goodList) {
                    goodListHtml = template("goodListTmp", goodList);
                    $(".list2-product-1 .list2-right").html(goodListHtml);
                });
                //tab栏    暂时没有
                // $("ul #longType").click(function () {
                //    var typeid = $(this).data("typepid");
                //     Ajax(0, '/api/good/list?limit=8&page=1&pid=' + typeid, 'get').then(function (goodList) {
                //         goodListHtml = template("goodListTmp", goodList);
                //         $(".list2-product-1 .list2-right").html(goodListHtml);
                //     });
                // });
            }
        });
    });
});

